這幾天研究下來,深感覺得儘管 Microsoft 嘗試擁抱開源,.NET Core 也釋出一段時間了,但是在非 Windows 平台上的資源還是少得可憐,就連一個 Code Linter 都找了一小時以上。
所幸,今天在尋找 Acceptance Testing 的套件時,發現老牌的 SpecFlow 在 3.x 的版本有支援 .NET Core,真是令人高興的消息,就讓我們來試玩看看吧!
首先,先以 Unit Test Project 的樣板建立一個以 Specs 結尾的專案名稱,並且把前紀上用不到的 UnitTest1.cs
砍掉。
$ cd Ironman/dotnet-sln/
$ dotnet new mstest -n WebProj.Specs
The template "Unit Test Project" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on WebProj.Specs/WebProj.Specs.csproj...
Restore completed in 16.06 sec for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj.
Restore succeeded.
$ cd WebProj.Specs/
$ rm UnitTest1.cs
接著在這個專案新增以下三個套件:
$ dotnet add package SpecFlow
Writing /var/folders/hx/lgdjx1l93pq08s35hhwb0py40000gn/T/tmp2EJTDI.tmp
info : Adding PackageReference for package 'SpecFlow' into project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Restoring packages for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/specflow/index.json
info : OK https://api.nuget.org/v3-flatcontainer/specflow/index.json 1091ms
info : GET https://api.nuget.org/v3-flatcontainer/specflow/3.0.225/specflow.3.0.225.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/specflow/3.0.225/specflow.3.0.225.nupkg 970ms
info : GET https://api.nuget.org/v3-flatcontainer/utf8json/index.json
info : GET https://api.nuget.org/v3-flatcontainer/bodi/index.json
info : GET https://api.nuget.org/v3-flatcontainer/gherkin/index.json
info : OK https://api.nuget.org/v3-flatcontainer/bodi/index.json 895ms
info : GET https://api.nuget.org/v3-flatcontainer/bodi/1.4.1/bodi.1.4.1.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/gherkin/index.json 927ms
info : GET https://api.nuget.org/v3-flatcontainer/gherkin/6.0.0/gherkin.6.0.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/utf8json/index.json 945ms
info : GET https://api.nuget.org/v3-flatcontainer/utf8json/1.3.7/utf8json.1.3.7.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/gherkin/6.0.0/gherkin.6.0.0.nupkg 11ms
info : OK https://api.nuget.org/v3-flatcontainer/utf8json/1.3.7/utf8json.1.3.7.nupkg 13ms
info : GET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/index.json
info : OK https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/index.json 235ms
info : GET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.4.0/system.threading.tasks.extensions.4.4.0.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.4.0/system.threading.tasks.extensions.4.4.0.nupkg 14ms
info : OK https://api.nuget.org/v3-flatcontainer/bodi/1.4.1/bodi.1.4.1.nupkg 14803ms
info : Installing System.Threading.Tasks.Extensions 4.4.0.
info : Installing BoDi 1.4.1.
info : Installing Utf8Json 1.3.7.
info : Installing Gherkin 6.0.0.
info : Installing SpecFlow 3.0.225.
info : Package 'SpecFlow' is compatible with all the specified frameworks in project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : PackageReference for package 'SpecFlow' version '3.0.225' added to file '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Committing restore...
info : Generating MSBuild file /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/WebProj.Specs.csproj.nuget.g.props.
info : Writing assets file to disk. Path: /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/project.assets.json
log : Restore completed in 19.02 sec for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj.
$ dotnet add package SpecFlow.Tools.MsBuild.Generation
Writing /var/folders/hx/lgdjx1l93pq08s35hhwb0py40000gn/T/tmpCbkpZ1.tmp
info : Adding PackageReference for package 'SpecFlow.Tools.MsBuild.Generation' into project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Restoring packages for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/specflow.tools.msbuild.generation/index.json
info : OK https://api.nuget.org/v3-flatcontainer/specflow.tools.msbuild.generation/index.json 1069ms
info : GET https://api.nuget.org/v3-flatcontainer/specflow.tools.msbuild.generation/3.0.225/specflow.tools.msbuild.generation.3.0.225.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/specflow.tools.msbuild.generation/3.0.225/specflow.tools.msbuild.generation.3.0.225.nupkg 972ms
info : Installing SpecFlow.Tools.MsBuild.Generation 3.0.225.
info : Package 'SpecFlow.Tools.MsBuild.Generation' is compatible with all the specified frameworks in project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : PackageReference for package 'SpecFlow.Tools.MsBuild.Generation' version '3.0.225' added to file '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Committing restore...
info : Generating MSBuild file /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/WebProj.Specs.csproj.nuget.g.props.
info : Generating MSBuild file /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/WebProj.Specs.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/project.assets.json
log : Restore completed in 5.46 sec for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj.
fntsr@fntsrde-MBP ~/P/I/d/WebProj.Specs> dotnet add package SpecRun.SpecFlow
Writing /var/folders/hx/lgdjx1l93pq08s35hhwb0py40000gn/T/tmp2yvaty.tmp
info : Adding PackageReference for package 'SpecRun.SpecFlow' into project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Restoring packages for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj...
info : GET https://api.nuget.org/v3-flatcontainer/specrun.specflow/index.json
info : OK https://api.nuget.org/v3-flatcontainer/specrun.specflow/index.json 1081ms
info : GET https://api.nuget.org/v3-flatcontainer/specrun.specflow/3.0.391/specrun.specflow.3.0.391.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/specrun.specflow/3.0.391/specrun.specflow.3.0.391.nupkg 873ms
info : GET https://api.nuget.org/v3-flatcontainer/specrun.specflow.3-0-0/index.json
info : OK https://api.nuget.org/v3-flatcontainer/specrun.specflow.3-0-0/index.json 887ms
info : GET https://api.nuget.org/v3-flatcontainer/specrun.specflow.3-0-0/3.0.391/specrun.specflow.3-0-0.3.0.391.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/specrun.specflow.3-0-0/3.0.391/specrun.specflow.3-0-0.3.0.391.nupkg 942ms
info : GET https://api.nuget.org/v3-flatcontainer/specrun.runner/index.json
info : OK https://api.nuget.org/v3-flatcontainer/specrun.runner/index.json 870ms
info : GET https://api.nuget.org/v3-flatcontainer/specrun.runner/3.0.391/specrun.runner.3.0.391.nupkg
info : OK https://api.nuget.org/v3-flatcontainer/specrun.runner/3.0.391/specrun.runner.3.0.391.nupkg 963ms
info : Installing SpecRun.Runner 3.0.391.
info : Installing SpecRun.SpecFlow.3-0-0 3.0.391.
info : Installing SpecRun.SpecFlow 3.0.391.
info : Package 'SpecRun.SpecFlow' is compatible with all the specified frameworks in project '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : PackageReference for package 'SpecRun.SpecFlow' version '3.0.391' added to file '/Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj'.
info : Committing restore...
info : Generating MSBuild file /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/WebProj.Specs.csproj.nuget.g.props.
info : Generating MSBuild file /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/WebProj.Specs.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/obj/project.assets.json
log : Restore completed in 11.32 sec for /Users/fntsr/Projects/Ironman/dotnet-sln/WebProj.Specs/WebProj.Specs.csproj.
接著在專案目錄下新增一個 *.feature
檔案:
Feature: Calculator
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers
@mytag
Scenario: Add two numbers
Given I have entered 50 into the calculator
And I have also entered 70 into the calculator
When I press add
Then the result should be 120 on the screen